home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / mgrtopbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  3.3 KB  |  130 lines

  1. /* mgrtopbm.c - read a MGR bitmap and produce a portable bitmap
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14. #include "mgr.h"
  15.  
  16. static void getinit ARGS(( FILE* file, int* colsP, int* rowsP, int* depthP, int* padrightP ));
  17. static bit getbit ARGS(( FILE* file ));
  18.  
  19. void
  20. main( argc, argv )
  21.     int argc;
  22.     char* argv[];
  23.     {
  24.     FILE* ifp;
  25.     bit* bitrow;
  26.     register bit* bP;
  27.     int rows, cols, depth, padright, row, col;
  28.  
  29.     pbm_init( &argc, argv );
  30.  
  31.     if ( argc > 2 )
  32.     pm_usage( "[mgrfile]" );
  33.  
  34.     if ( argc == 2 )
  35.     ifp = pm_openr( argv[1] );
  36.     else
  37.     ifp = stdin;
  38.  
  39.     getinit( ifp, &cols, &rows, &depth, &padright );
  40.     if ( depth != 1 )
  41.     pm_error( "MGR file has depth of %d, must be 1", depth );
  42.  
  43.     pbm_writepbminit( stdout, cols, rows, 0 );
  44.     bitrow = pbm_allocrow( cols );
  45.  
  46.     for ( row = 0; row < rows; row++ )
  47.     {
  48.     /* Get data, bit-reversed within each byte. */
  49.         for ( col = 0, bP = bitrow; col < cols; col++, bP++ )
  50.         *bP = getbit( ifp );
  51.     /* Discard line padding */
  52.         for ( col = 0; col < padright; col ++ )
  53.         (void) getbit( ifp );
  54.     pbm_writepbmrow( stdout, bitrow, cols, 0 );
  55.     }
  56.  
  57.     pm_close( ifp );
  58.     pm_close( stdout );
  59.  
  60.     exit( 0 );
  61.     }
  62.  
  63.  
  64. static unsigned char item;
  65. static int bitsperitem, bitshift;
  66.  
  67. static void
  68. getinit( file, colsP, rowsP, depthP, padrightP )
  69.     FILE* file;
  70.     int* colsP;
  71.     int* rowsP;
  72.     int* depthP;
  73.     int* padrightP;
  74.     {
  75.     struct b_header head;
  76.     int pad;
  77.  
  78.     if ( fread( &head, sizeof(struct old_b_header), 1, file ) != 1 )
  79.     pm_perror( "reading header" );
  80.     if ( head.magic[0] == 'y' && head.magic[1] == 'z' )
  81.     { /* new style bitmap */
  82.     if ( fread( &head.depth, sizeof(head) - sizeof(struct old_b_header), 1, file ) != 1 )
  83.         pm_perror( "reading rest of header" );
  84.     *depthP = (int) head.depth - ' ';
  85.     pad = 8;
  86.     }
  87.     else if ( head.magic[0] == 'x' && head.magic[1] == 'z' )
  88.     { /* old style bitmap with 32-bit padding */
  89.     *depthP = 1;
  90.     pad = 32;
  91.     }
  92.     else if ( head.magic[0] == 'z' && head.magic[1] == 'z' )
  93.     { /* old style bitmap with 16-bit padding */
  94.     *depthP = 1;
  95.     pad = 16;
  96.     }
  97.     else if ( head.magic[0] == 'z' && head.magic[1] == 'y' )
  98.     { /* old style 8-bit pixmap with 16-bit padding */
  99.     *depthP = 8;
  100.     pad = 16;
  101.     }
  102.     else
  103.     pm_error(
  104.         "bad magic chars in MGR file: '%c%c'",
  105.         head.magic[0], head.magic[1] );
  106.     *colsP = ( ( (int) head.h_wide - ' ' ) << 6 ) + ( (int) head.l_wide - ' ' );
  107.     *rowsP = ( ( (int) head.h_high - ' ' ) << 6 ) + ( (int) head.l_high - ' ' );
  108.     *padrightP = ( ( *colsP + pad - 1 ) / pad ) * pad - *colsP;
  109.  
  110.     bitsperitem = 8;
  111.     }
  112.  
  113. static bit
  114. getbit( file )
  115.     FILE* file;
  116.     {
  117.     bit b;
  118.  
  119.     if ( bitsperitem == 8 )
  120.     {
  121.     item = getc( file );
  122.     bitsperitem = 0;
  123.     bitshift = 7;
  124.     }
  125.     bitsperitem++;
  126.     b = ( ( item >> bitshift) & 1 ) ? PBM_BLACK : PBM_WHITE;
  127.     bitshift--;
  128.     return b;
  129.     }
  130.